<html>
<head>
  <meta charset="utf-8">
  <title>我的服务</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../js/layui/css/layui.css"  media="all">
</head>
<body>
 
<table class="layui-hide" id="test" lay-filter="test"></table>
 
 
<script type="text/html" id="barDemo">
	<a class="layui-btn layui-btn-xs" lay-event="accept">同意</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="refuse">拒绝</a>
</script>
              
          
<script src="../js/layui/layui.js" charset="utf-8"></script> 
<script src="../js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script>
	// 获取该医生的电话号码
	var phone = localStorage.getItem("FDSMS");
	// 加载模块
	layui.use('table', function(){
  var table = layui.table;
  // 表格加载
  table.render({
    elem: '#test'
    ,url:'http://123.56.67.48:8080/getWaitedAg?phone='+phone//http://homedoc:8881/tp5/public/index.php/Doc/doc/mypatient
		,request: {
		   pageName: 'page' //页码的参数名称，默认：page
		   ,limitName: 'limit' //每页数据量的参数名，默认：limit
		 }
    // ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
    ,defaultToolbar: ['filter', 'print', { 
      title: '提示'
      ,layEvent: 'LAYTABLE_TIPS'
      ,icon: 'layui-icon-tips'
    }]
    ,title: '所有签约人信息'
    ,cols: [[
      {field:'patName', title:'姓名', width:120,}
      ,{field:'patPhone', title:'联系电话', width:150,}
      ,{field:'patAddr', title:'所在城市', width:150,}
			,{field:'patAddr', title:'所在城市', width:150,}
			,{field:'agNum', title:'合同编号', width:120,}
      ,{field:'pakName', title:'签约服务包', width:120}
      ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:360}
    ]]
    ,page: true
  });
  
  
  //监听行工具事件
  table.on('tool(test)', function(obj){
		//获取合同编号，准备向后台传输
    var data = obj.data;
    //console.log(obj)
    if(obj.event === 'accept'){
			var k = JSON.stringify({
							phone:phone,
							agNum:data.agNum
						});
			console.log(k);
			// 同意就向后端发送同意的指令，后端更改状态合约状态码
       $.ajax({
				url:'http://123.56.67.48:8080/docSign',
				method:'POST',
				contentType:'application/json',
				data:JSON.stringify({
					phone:phone,
					agNum:data.agNum
				}),
				success:function(res){
					if(JSON.parse(res).code == "1"){
						alert("合约生效");
						table.reload('test', {
						  page: {
						    curr: 1 //重新从第 1 页开始
						  }
						}); //只重载数据
					}
				},
				fail:function(){
					console.log("fail");
				}
			});
    } else if(obj.event === 'refuse'){
			var k = JSON.stringify({
							phone:phone,
							agNum:data.agNum
						});
			console.log(k);
      layer.confirm('真的拒绝该用户合约吗?', function(index){
					$.ajax({
						url:'http://123.56.67.48:8080/cancelSign',
						method:'POST',
						contentType:'application/json',
						data:k,
						success:function(res){
							if(JSON.parse(res).code == "1"){
								alert("你拒绝了该合约");
							}
						},
						fail:function(){
							console.log("fail");
						}
					});
      		obj.del();
      		layer.close(index);
      });
    }
  });
});
</script>

</body>
</html>